/* @(#)UploadFiles.java    V1.0
 *
 * Project: APEP 
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   wangp    {CREATE_TIME} First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd. 
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *   
 * 
 */
package com.apep.gw.synch;

import java.io.PrintWriter;

import com.apep.gw.data.UploadFilesPageData;

import framework.core.DBConnection;
import framework.core.IHandler;
import framework.core.ILoginContext;
import framework.core.Logger;
import framework.core.PageData;
import framework.core.SearchEvent;
import framework.core.SrvContext;
import framework.core.Value;
/*
	Author    :wangp
	CreateTime:{CREATE_TIME}
*/
public class UploadFilesSrv extends IHandler
{

	public UploadFilesSrv() 
	{
		super();
	}
	private void deleteUploadFiles(DBConnection con,SrvContext context) throws Exception
	{
		int[] ids = context.getInts("ids");
		UploadFiles obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = UploadFiles.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveUploadFiles(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		int id = context.getInt("Id",Value.NULL_INT);
		UploadFiles obj;
		if(id==Value.NULL_INT)   //add
		{
			obj = UploadFiles.fetch();
    	}
    	else                     //update
    	{
    		obj = UploadFiles.fetch(con,id);
    	}
		UploadFilesPageData data = (UploadFilesPageData)PageData.fromForm(context,UploadFilesPageData.class);
		context.setValue("UploadFilesPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listUploadFiles(DBConnection con,SrvContext context,String path) throws Exception
	{
		UploadFilesSearchEvent event = (UploadFilesSearchEvent)SearchEvent.fromForm(context,UploadFilesSearchEvent.class);
		context.setSessionValue("UploadFilesSearchEvent",event);
		context.setNextUrl(path+"UploadFilesList.jsp");
	}
	private void backUploadFiles(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"UploadFilesList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "";
		
		if(action_code.equalsIgnoreCase("listUploadFiles"))
		{
			listUploadFiles(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewUploadFiles"))
		{
			int id = context.getInt("Id");
			UploadFiles obj = UploadFiles.fetch(con,id);
			context.setValue("UploadFiles",obj);
			context.setNextUrl(path+"UploadFilesView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteUploadFiles"))
		{
			context.setErrorUrl(path+"UploadFilesList.jsp");
			deleteUploadFiles(con,context);
			backUploadFiles(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddUploadFiles"))
		{
			UploadFiles obj = UploadFiles.fetch();
			UploadFilesPageData data = (UploadFilesPageData)PageData.fromTable(obj,UploadFilesPageData.class);
			context.setValue("UploadFilesPageData",data);
			context.setNextUrl(path+"UploadFilesEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateUploadFiles"))
		{
			int id = context.getInt("Id");
			UploadFiles obj = UploadFiles.fetch(con,id);
			UploadFilesPageData data = (UploadFilesPageData)PageData.fromTable(obj,UploadFilesPageData.class);
			context.setValue("UploadFilesPageData",data);
			context.setNextUrl(path+"UploadFilesEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyUploadFiles"))
		{
			int id = context.getInt("Id");
			UploadFiles obj = UploadFiles.copy(UploadFiles.fetch(con,id));
			UploadFilesPageData data = (UploadFilesPageData)PageData.fromTable(obj,UploadFilesPageData.class);
			context.setValue("UploadFilesPageData",data);
			context.setNextUrl(path+"UploadFilesEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveUploadFiles"))
		{
			context.setErrorUrl(path+"UploadFilesEdit.jsp");
			saveUploadFiles(con,context,loginContext);
			backUploadFiles(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewUploadFiles"))
		{
			context.setErrorUrl(path+"UploadFilesEdit.jsp");
			saveUploadFiles(con,context,loginContext);
			UploadFiles obj = UploadFiles.fetch();
			UploadFilesPageData data = (UploadFilesPageData)PageData.fromTable(obj,UploadFilesPageData.class);
			context.setValue("UploadFilesPageData",data);
			context.setNextUrl(path+"UploadFilesEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListUploadFiles"))
		{
			backUploadFiles(con,context,path);
			return;
		}
		
		//add by wangp start
        if(action_code.equalsIgnoreCase("requestDownload"))
        {
            String lastEditTime = context.getString("LastEditTime");
            
            PrintWriter tOutput = context.m_res.getWriter();
            
            if(DownloadFiles.generateDownloadFiles(con, lastEditTime))
                tOutput.write("OK");
            else
                tOutput.write("Error");
            return;
        }
		//add by wangp end

		Logger.throwException("UploadFilesSrv","unknown action_code="+action_code);
	}
}
